package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.RecyclerViewItemDecoration;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;
import com.app.view.BaseViewStateLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;
import phone.qyg.com.qyg.model.Find;
import phone.qyg.com.qyg.utils.FindUtil;
import phone.qyg.com.qyg.utils.PopuType;

/**
 * 作者：lblbh on 2017/12/5 15:07
 * 邮箱：lanbuhan@163.com
 * 功能：全局搜索
 */
@ContentView(R.layout.app_find)
public class AppFindActivity extends BaseActivity {

    @ViewInject(R.id.type_view)
    TextView type_view;

    @ViewInject(R.id.quit)
    TextView quit;

    @ViewInject(R.id.find)
    TextView find;

    @ViewInject(R.id.clear_record)
    TextView clear_record;

    @ViewInject(R.id.type_popu)
    TextView type_popu;

    @ViewInject(R.id.find_edit)
    EditText find_edit;

    @ViewInject(R.id.linear)
    LinearLayout linear;

    BaseViewStateLayout baseViewLayout;

    CommonAdapter adapter;

    RecyclerView recycler;

    List<Base> searchTypeTheme;

    Base selectTheme;

    @Override
    public void initSucessView() {

        searchTypeTheme = JSONTool.jsonDefaluTranClazzs( SharePrefUtil.getString(SharePrefUtil.KEY.APP_INFO,""),JSONTool.Enum.GLOBAL_SEARCH_TYPE,Base.class);
        Object obj = getTransModels();

        if(obj instanceof Base){
            Base tempBase = (Base) obj;

            for (int i=0,j=searchTypeTheme.size();i<j;i++){
                Base itemBase = searchTypeTheme.get(i);
                if(itemBase.title.equals(tempBase.title)){
                    selectTheme = itemBase;
                    break;
                }
            }
        }else{
            if(isRequestList(searchTypeTheme))
                selectTheme = searchTypeTheme.get(0);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(selectTheme != null)
            type_view.setText(selectTheme.title);

        baseViewLayout = new BaseViewStateLayout(QXApplication.getContext()) {

            @Override
            public View obtionView() {
                View sucessView = View.inflate(QXApplication.getContext(), R.layout.find_recycler, null);
                recycler = (RecyclerView) sucessView.findViewById(R.id.recycler);
                return sucessView;
            }

            @Override
            public Object obtionData() {
                final List<Find> finds = FindUtil.findData();
                CommonUtil.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        setRecordData(finds);
                    }
                });
                return DELFAUTSTATE;
            }
        };

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linear.addView(baseViewLayout, layoutParams);
        x.view().inject(linear);

        find_edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    find();
                    return true;
                }
                return false;
            }
        });
    }


    private void setRecordData(List<Find> models){
        if (!isRequestList(models)) {
            baseViewLayout.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
            baseViewLayout.stateView();
            clear_record.setVisibility(View.GONE);
            return;
        }

        baseViewLayout.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_SUCESS);
        baseViewLayout.stateView();

        GridLayoutManager girdLayoutManager = new GridLayoutManager(QXApplication.getContext(),2,GridLayoutManager.VERTICAL,false);
        recycler.addItemDecoration(new RecyclerViewItemDecoration(
                    RecyclerViewItemDecoration.MODE_GRID,
                    getResources().getColor(R.color.divider),
                    CommonUtil.dip2px(QXApplication.getContext(),1),0,0));
        recycler.setLayoutManager(girdLayoutManager);


        final CommonAdapter adapter = new CommonAdapter<Find>(QXApplication.getContext(), R.layout.item_find_record, models) {
            @Override
            protected void convert(ViewHolder holder, final Find model, final int position) {
                holder.setText(R.id.find_key, model.getKey());
            }
        };


        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Find find = (Find) adapter.getData().get(position);
                find.base = selectTheme;
                TransformController.transformControllerModel(QXApplication.getContext(),FindEndActivity.class,find);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        recycler.setAdapter(adapter);
    }

    @Event(value = {R.id.quit,R.id.type_view,R.id.clear_record,R.id.find})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.quit:
                this.finish();
                break;
            case R.id.type_view:
                PopuType.setPopuThemes(this, type_popu, searchTypeTheme, selectTheme, new PopuType.OnClickState() {
                    @Override
                    public void OnItemClick(Object model) {
                        Base base = (Base) model;
                        selectTheme = base;
                        type_view.setText(selectTheme.title);
                    }
                });
                break;
            case R.id.clear_record:
                FindUtil.deleteFind();
                baseViewLayout.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
                baseViewLayout.stateView();
                break;
            case R.id.find:
                find();
                break;
        }
    }

    private void find(){
        String key = getEditToString(find_edit);

        if(!isRequestStr(key) || key.equals(getString(R.string.please_find_key))){
            PromptManager.showToast(R.string.please_add_find_key);
            return;
        }else{
            FindUtil.addFindKey(key);
        }
        Find find = new Find(key);
        find.base = selectTheme;

        TransformController.transformControllerModel(QXApplication.getContext(),FindEndActivity.class,find);
    }

    @Override
    public void initViewHandler() {
    }
}
